const float RANGE = .5;

V2DF * g_selected = 0;

V2DF g_points[] =
{
				V2DF(0,0.55),
				V2DF(0,0.2),
	V2DF(-0.7,0),			V2DF(0.7,0),
				V2DF(0,-0.4),
	V2DF(-0.5,-0.9),		V2DF(0.5,-0.9)
};
const int g_pointsSize = sizeof(g_points)/sizeof(g_points[0]);
#define P_HEAD	0
#define P_NECK	1
#define P_LARM	2
#define P_RARM	3
#define P_BELT	4
#define P_LLEG	5
#define P_RLEG	6

struct Segment
{
	V2DF * start, * end;
	float length;
	Segment():start(0),end(0),length(0){}
	void set(V2DF * s, V2DF * e){start=s;end=e;length=start->distance(*end);}
	Segment(V2DF * start, V2DF * end){set(start,end);}
	void draw(){start->glDrawTo(*end);}
	void update()
	{
		float currentLength = start->distance(*end);
		float difference = currentLength - length;
		if(difference != 0)
		{
			V2DF midpoint = V2DF::between(*start, *end);
			float halfDist = length/2;
			V2DF angle = start->difference(*end);
			angle.normalize();
			angle.multiply(halfDist);
			start->set(midpoint.sum(angle));
			end->set(midpoint.difference(angle));
		}
	}
};

struct Body
{
	TemplateVector<Segment> m_body;
	void init()
	{
		m_body.setSize(6);
		m_body.get(0).set(&g_points[P_HEAD],&g_points[P_NECK]);
		m_body.get(1).set(&g_points[P_NECK],&g_points[P_LARM]);
		m_body.get(2).set(&g_points[P_NECK],&g_points[P_RARM]);
		m_body.get(3).set(&g_points[P_NECK],&g_points[P_BELT]);
		m_body.get(4).set(&g_points[P_BELT],&g_points[P_LLEG]);
		m_body.get(5).set(&g_points[P_BELT],&g_points[P_RLEG]);
	}
	void update()
	{
		for(int i = 0; i < m_body.size(); ++i)
		{
			m_body.get(i).update();
		}
	}
	void draw()
	{
		for(int i = 0; i < m_body.size(); ++i)
		{
			m_body.get(i).draw();
		}
		glDrawCircle(g_points[P_HEAD], 0.3);
	}
};

Body g_body;

